﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using DevExpress.XtraGrid.Views.Grid;

namespace Client
{
    public partial class FormVipManager : BaseChildForm
    {
        QunYing.BLL.VipInfo vipInfo = new QunYing.BLL.VipInfo();
        DataTable dt;
        public FormVipManager()
        {
            InitializeComponent();
            this.sharpGrid1.ShowRowNumIndicator = true;
            this.sharpGrid1.DeleteGridRowEvent += new Spore.Controls.DeleteGridRowEventHandler(sharpGrid1_DeleteGridRowEvent);
            this.sharpGrid1.RowDoubleClickEvent += new EventHandler<Spore.Controls.RowDoubleClickEventArg>(sharpGrid1_RowDoubleClickEvent);
            this.sharpGrid1.AllowPopupMenu = true;
            this.sharpGrid1.PopupMenuStrip.Opening += new CancelEventHandler(PopupMenuStrip_Opening);
        }

        void PopupMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            DataRow dr = this.GetDataRow();
            if (dr == null)
            {
                this.GetMenuItem("修改申报信息").Visible = false;
            }
            if (dr["审核状态"].ToString() == "审核通过")
            {
                this.GetMenuItem("修改申报信息").Visible = false;
            }
            else
            {
                this.GetMenuItem("修改申报信息").Visible = true;
            }
        }

        private ToolStripMenuItem GetMenuItem(string text)
        {
            foreach (var v in this.sharpGrid1.PopupMenuStrip.Items)
            {
                ToolStripMenuItem item = v as ToolStripMenuItem;
                if (item == null)
                {
                    continue;
                }
                if (item.Text == text)
                {
                    return item;
                }
            }
            return null;
        }

        void sharpGrid1_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    QunYing.Model.VipInfo vi = vipInfo.GetModel(row.Row["会员卡号"].ToString());
                    VipLook form = SingleObjManager.GetSingleObj<VipLook>();
                    form.BindVipInfo(vi);
                    form.ShowDialog();
                    break;
                }
            }
        }

        void sharpGrid1_DeleteGridRowEvent(DevExpress.XtraGrid.Views.Base.BaseView bView)
        {
            this.RemoveVip(null, EventArgs.Empty);
        }
        public override void BindData()
        {
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                this.dt = vipInfo.GetList(string.Format("经销商编号='{0}'", GlobalInfo.UserLoginInfo.用户ID)).Tables[0];
                this.sharpGrid1.DataSource = dt;
            }
            else if (GlobalInfo.UserLoginInfo.角色 == "平台管理员")
            {
                this.dt = vipInfo.GetAllList().Tables[0];
                this.sharpGrid1.DataSource = dt;

            }
            ((GridView)this.sharpGrid1.MainView).Columns["地址图片"].Visible = false;
            ((GridView)this.sharpGrid1.MainView).Columns["首次缴费"].Visible = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            this.sharpGrid1.AllowPopupMenu = true;
            this.sharpGrid1.AddPopupMenu(new ToolStripSeparator(), null);
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                this.dt = vipInfo.GetList(string.Format("经销商编号='{0}'", GlobalInfo.UserLoginInfo.用户ID)).Tables[0];
                this.sharpGrid1.DataSource = dt;
                this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("会员续费"), VipDefer);
                this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("增加设备"), (vv, v) => AddDevice());
                this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("修改申报信息"),(vv,v)=>ModifyReportInfo());

            }
            else if(GlobalInfo.UserLoginInfo.角色=="平台管理员")
            {
                  this.dt=vipInfo.GetAllList().Tables[0];
                  this.sharpGrid1.DataSource = dt;
            }
            this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("修改会员资料"), ModifyVipInfo);
           this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("会员设备信息"), ShowICSAInfo);
           this.sharpGrid1.AddPopupMenu( new ToolStripMenuItem( "删除选择会员"), RemoveVip);
           ((GridView)this.sharpGrid1.MainView).Columns["地址图片"].Visible = false;
           ((GridView)this.sharpGrid1.MainView).Columns["首次缴费"].Visible = false;
            GridView gv = this.sharpGrid1.MainView as GridView;
            base.OnLoad(e);
        }

        private void ModifyReportInfo()
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    QunYing.Model.VipInfo model = vipInfo.GetModel(row.Row["会员卡号"].ToString());
                    if (model != null)
                    {
                        VipAddModify form = SingleObjManager.GetSingleObj<VipAddModify>();
                        form.BindData(model);
                        if (form.ShowDialog() == DialogResult.OK)
                            this.BindData();
                    }
                    break;
                }
            }
        }

        private void AddDevice()
        {
            var datarow = this.GetDataRow();
            if (datarow == null)
            {
                GlobalInfo.MsgBoxShowError("请选择一个客户！");
                return;
            }
            DataTable dt = datarow.Table.Clone();
            dt.ImportRow(datarow);
            var vip = vipInfo.DataTableToList(dt)[0];
            FormAddDevice form = SingleObjManager.GetSingleObj<FormAddDevice>();
            form.BindData(vip);
            if (form.ShowDialog() == DialogResult.OK)
            {
                datarow["水族箱设备ID"] = vip.水族箱设备ID;
                this.sharpGrid1.DefaultView.RefreshData();
            }
        }

        private void ModifyVipInfo(object o, EventArgs e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    QunYing.Model.VipInfo model = vipInfo.GetModel(row.Row["会员卡号"].ToString());
                    if (model != null)
                    {
                        VipModify form = SingleObjManager.GetSingleObj<VipModify>();
                        form.BindData(model);
                        if (form.ShowDialog() == DialogResult.OK)
                            this.BindData();
                    }
                    break;
                }
            }
        }

        private void VipDefer(object sender, EventArgs e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                   QunYing.Model.VipInfo model= vipInfo.GetModel(row.Row["会员卡号"].ToString());
                   if (model != null)
                   {
                       FormVipDeferReq form = SingleObjManager.GetSingleObj<FormVipDeferReq>();
                       form.BindData(model);
                       form.ShowDialog();
                   }
                    break;
                }
            }
        }

        private void ShowICSAInfo(object sender,EventArgs e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    string[] str = row.Row["水族箱设备ID"].ToString().Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
                    //QunYing.Model.VipInfo vi = vipInfo.GetModel(row.Row["会员卡号"].ToString());
                    QunYing.BLL.ICSA_Device bll_ICsa = new QunYing.BLL.ICSA_Device();
                    List<QunYing.Model.ICSA_Device> devices = new List<QunYing.Model.ICSA_Device>();
                    foreach (var vvv in str)
                    {
                        QunYing.Model.ICSA_Device dev = bll_ICsa.GetModel(vvv);
                        if (dev != null)
                            devices.Add(dev);
                    }
                    if (devices.Count > 0)
                    {
                        FormICSALook form = SingleObjManager.GetSingleObj<FormICSALook>();
                        form.BindData(devices);
                        form.ShowDialog();
                    }
                    else
                    {
                        GlobalInfo.MsgBoxShow("该会员没用设备信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    break;
                }
            }
        }

        private DataRow GetDataRow()
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] rows = view.GetSelectedRows();
            if (rows.Length == 0)
                return null;
            foreach (var v in rows)
            {
                if (v < 0)
                    continue;
                DataRowView rowView = view.GetRow(v) as DataRowView;
                if (rowView == null)
                    return null;
                return rowView.Row;
            }
            return null;

        }

        private void RemoveVip(object sender,EventArgs e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            if (selectedRows.Length == 0)
            {
                GlobalInfo.MsgBoxShowError("请选择你要删除的会员");
                return;
            }
            else
            {
               if( XtraMessageBox.Show( GlobalInfo.UserLookAndFeel,"确定要删除会员吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)== DialogResult.No)
                return;
                foreach (var v in selectedRows)
                {
                   object obj= view.GetRow(v);
                   DataRowView row = obj as DataRowView;
                   if (row == null)
                       continue;
                   vipInfo.Delete(row.Row["会员卡号"].ToString());
                }
                if (GlobalInfo.UserLoginInfo.角色 == "经销商")
                {
                    this.sharpGrid1.BeginUpdate();
                    this.sharpGrid1.DataSource = vipInfo.GetList(string.Format("经销商编号='{0}'", GlobalInfo.UserLoginInfo.用户ID)).Tables[0];
                    view.Columns["地址图片"].Visible = false;
                    this.sharpGrid1.EndInit();
                }
            }
        }

        protected override void OnServiceInfo(Message msg)
        {
            
            //ServiceRet<DataTable> dt = WinMsgHelper.GetValue<ServiceRet<DataTable>>(msg.WParam.ToInt32());
            //this.sharpGrid1.DataSource = dt.Result;
        }

        private void btn_Selected_Click(object sender, EventArgs e)
        {
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                this.dt = vipInfo.GetList(string.Format("经销商编号='{0}'", GlobalInfo.UserLoginInfo.用户ID)).Tables[0];
            }
            else if (GlobalInfo.UserLoginInfo.角色 == "平台管理员")
            {
                this.dt = vipInfo.GetAllList().Tables[0];
            }
            List<DataRow> drs = new List<DataRow>();
            bool flag1=false;
            bool flag2 = false;
           
            if (this.sharpDateTimeSpan1.StartDateEdit.DateTime != DateTime.MinValue && this.sharpDateTimeSpan1.EndDateEdit.DateTime != DateTime.MinValue)
            {
                flag1 = true;
            }
            if (this.sharpDateTimeSpan2.StartDateEdit.DateTime != DateTime.MinValue && this.sharpDateTimeSpan2.EndDateEdit.DateTime != DateTime.MinValue)
            {
                flag2 = true;
            }
            foreach (DataRow v in this.dt.Rows)
            {
                DateTime dt1 = (DateTime)v["入会时间"];
                DateTime dt2 = (DateTime)v["到期时间"];
                if (flag1)
                {
                    if (dt1 >= sharpDateTimeSpan1.StartDateEdit.DateTime && dt1 <= this.sharpDateTimeSpan1.EndDateEdit.DateTime)
                    {
                        if (flag2)
                        {
                            if (dt2 >= sharpDateTimeSpan2.StartDateEdit.DateTime && dt2 <= this.sharpDateTimeSpan2.EndDateEdit.DateTime)
                            {
                                continue;
                            }
                            else
                            {
                                drs.Add(v);
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        drs.Add(v);
                        continue;
                    }
                }
                else
                {
                    if (flag2)
                    {
                        if (dt2 >= sharpDateTimeSpan2.StartDateEdit.DateTime && dt2 <= this.sharpDateTimeSpan2.EndDateEdit.DateTime)
                        {
                            continue;
                        }
                        else
                        {
                            drs.Add(v);
                        }
                    }
                }
            }
            foreach (var v in drs)
            {
                this.dt.Rows.Remove(v);
            }
            this.sharpGrid1.DataSource = this.dt;
        }
    }
}